<template>
	<view>
		<view class="list">
			<view class="list-one" v-for="(item, list) in consultlist" :key="list" @click="click_listone(item)">
				<view class="left">
					<u--image width="236rpx" height="236rpx"
						:src="item.pics[0] ? item.pics[0] : 'http://cdn.uviewui.com/uview/empty/data.png'" radius="4">
						<template v-slot:loading>
							<u-loading-icon color="#39488a"></u-loading-icon>
						</template>
					</u--image>
				</view>
				<view class="right">
					<view class="tit two">{{ item.title }}</view>
					<view class="bottm">
						<u-parse :content="item.info"></u-parse>
					</view>
					<view class="time">{{ item.createTime }}</view>
				</view>
			</view>
		</view>
		<u-empty :show="consultlist.length == 0 && loading ? true : false" marginTop="100"></u-empty>
		<u-loadmore :status="loadstatus" line v-if="consultlist.length > 0" />
		<view style="height: 140rpx"></view>
		<view class="btn" @click="show = true">
			<view class="topval">发布</view>
			<view class="bottmval">邻里互助</view>
		</view>
		<u-popup :show="show" @close="close" :round="10" closeable>
			<view class="popup-tit">发布邻里互助</view>
			<view class="popup-cont">
				<view class="tit">发布标题</view>
				<view class="text">
					<u--input v-model="formdata.title" placeholder="请输入发布标题"></u--input>
				</view>
				<view class="tit">详细内容</view>
				<view class="text">
					<u--textarea v-model="formdata.info" placeholder="请输入内容" count></u--textarea>
				</view>
				<view class="tit">联系人</view>
				<view class="text">
					<u--input v-model="formdata.contactNickname" placeholder="请输入联系人"></u--input>
				</view>
				<view class="tit">联系电话</view>
				<view class="text">
					<u--input v-model="formdata.contactMobile" placeholder="请输入联系电话"></u--input>
				</view>
				<view class="tit">照片或视频</view>
				<u-upload :fileList="fileList2" @afterRead="afterRead" @delete="deletePic" :previewFullImage="false" name="2" :maxCount="9"
					accept="file" uploadText="照片或视频" :sizeType="['compressed']"></u-upload>
				<view style="color: red;font-size: 24rpx;" v-if="errtext">{{errtext}}</view>
				<view class="btns" @click="click_idea">发布</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		llhzpage,
		llhzcreate
	} from '@/api/index.js';
	import {
		commonUpload
	} from '@/api/mine.js';
	export default {
		data() {
			return {
				consultlist: [],
				queryParams: {
					pageNo: 1,
					pageSize: 10
				},
				show: false,
				fileList2: [],
				loading: false,
				loadstatus: 'loading',
				formdata: {
					title: '',
					info: '',
					contactNickname: '',
					contactMobile: '',
					pics: []
				},
				errtext: ''
			};
		},
		onLoad() {
			this.get_zjzx();
		},
		methods: {
			click_idea() {
				if (!this.formdata.title || !this.formdata.info || !this.formdata.contactNickname || !this.formdata
					.contactMobile) {
					uni.$u.toast('请填写内容')
					return;
				}
				if (this.fileList2.length > 0) {
					for (var i = 0; i < this.fileList2.length; i++) {
						this.formdata.pics.push(this.fileList2[i].url);
					}
				}
				llhzcreate(this.formdata).then((res) => {
					if (res.code == '0') {
						uni.$u.toast('发布成功，请等待审核');
						this.queryParams.pageNo = 1;
						this.consultlist = [];
						this.get_zjzx();
						this.close();
					} else {
						uni.$u.toast(res.msg);
						// this.errtext = res.msg
					}
				});
			},
			close() {
				this.show = false;
				this.fileList2 = [];
				this.errtext = ''
				this.formdata = {
					title: '',
					info: '',
					contactNickname: '',
					contactMobile: '',
					pics: []
				}
			},
			get_zjzx() {
				llhzpage(this.queryParams).then((res) => {
					this.loading = true;
					if (res.data.list.length == 0 && this.queryParams.pageNo > 1) {
						this.loadstatus = 'nomore';
						return;
					}
					if (this.queryParams.pageNo == 1 && res.data.list.length < 10) {
						this.loadstatus = 'nomore';
					}
					this.consultlist = this.consultlist.concat(res.data.list);
					this.queryParams.pageNo = res.data.list.length == 0 ? this.queryParams.pageNo : (this
						.queryParams.pageNo += 1);
				});
			},
			click_listone(list) {
				uni.navigateTo({
					url: '/pages/index/neighbor/details/details?id=' + list.id
				});
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1);
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file);
				let fileListLen = this[`fileList${event.name}`].length;
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					});
				});
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url);
					let item = this[`fileList${event.name}`][fileListLen];
					this[`fileList${event.name}`].splice(
						fileListLen,
						1,
						Object.assign(item, {
							status: 'success',
							message: '',
							url: result
						})
					);
					fileListLen++;
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = commonUpload({
						url: url
					}).then((res) => {
						if (res.code == '0') {
							setTimeout(() => {
								resolve(res.data);
							}, 50);
						} else {
							uni.$u.toast('上传失败');
						}
					});
				});
			}
		},
		onPullDownRefresh() {
			this.queryParams.pageNo = 1;
			this.consultlist = [];
			this.get_zjzx();
		},
		onReachBottom() {
			this.get_zjzx();
			this.loadstatus = 'loading';
		}
	};
</script>

<style lang="scss">
	.list {
		padding: 24rpx;

		.list-one {
			padding: 12rpx;
			display: flex;
			background: #FFFFFF;
			box-shadow: 0px 8rpx 40rpx 0px rgba(0, 0, 0, 0.1);
			border-radius: 12rpx;
			margin-bottom: 20rpx;

			.left {
				margin-right: 22rpx;

				image {
					width: 236rpx;
					height: 236rpx;
				}
			}

			.right {
				flex: auto;

				.tit {
					font-size: 28rpx;
					font-weight: bold;
					line-height: 36rpx;
					min-height: 72rpx;
				}

				.bottm {
					margin-top: 12rpx;
					font-size: 24rpx;
					line-height: 36rpx;
					color: #666666;
					min-height: 108rpx;
				}

				.red {
					color: red;
				}

				.time {
					margin-top: 12rpx;
					color: #666666;
					font-size: 24rpx;
					line-height: 36rpx;
				}
			}
		}
	}

	.btn {
		position: fixed;
		top: 50%;
		z-index: 9;
		right: 12rpx;
		width: 182rpx;
		height: 206rpx;
		text-align: center;
		background: url('../../../static/index/btn.png') no-repeat;
		background-size: 100% 100%;
		color: #3C6D09;
		font-size: 24rpx;

		.topval {
			text-indent: 1em;
			margin-top: 120rpx;
			line-height: 36rpx;
		}

		.bottmval {
			text-indent: 1em;
			line-height: 36rpx;
		}
	}

	.popup-tit {
		line-height: 44rpx;
		padding-top: 48rpx;
		text-align: center;
	}

	.popup-cont {
		padding: 24rpx 48rpx 48rpx;
		max-height: 800rpx;
		overflow: auto;

		.tit {
			text-align: left;
			font-size: 26rpx;
			line-height: 44rpx;
			font-weight: bold;
			margin-top: 12rpx;
			margin-bottom: 12rpx;
		}

		.text {
			margin: 0;
			border: 2rpx solid #d8d8d8;
			border-radius: 12rpx;
		}

		.btns {
			margin: 64rpx;
			height: 88rpx;
			background: #8BB950;
			border-radius: 20rpx;
			color: #fff;
			text-align: center;
			line-height: 88rpx;
		}
	}
</style>